package test4;

import java.util.Arrays;

/**
 * 前缀匹配优化
 *
 * @Author Jun jie Yi
 * 2022/5/1 22:16
 */
public class NumArray {
    static int[] arr;

    public static void main(String[] args) {
        int[][] nums = {{-2, 0, 3, -5, 2, -1}, {0, 2}, {2, 5}, {0, 5}};
        new NumArray(nums[0]);
        for (int i = 1; i < nums.length; i++) {
            System.out.println(sumRange(nums[i][0], nums[i][1]));
        }
    }

    public NumArray(int[] nums) {
        arr = new int[nums.length + 1];
        for (int i = 1; i < arr.length; i++) {
            arr[i] = nums[i - 1] + arr[i - 1];
        }
    }

    public static int sumRange(int left, int right) {
        return arr[right + 1] - arr[left];
    }
}
